package com.spc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spc.dao.MapDao;
import com.spc.entity.PageResult;
import com.spc.entity.QueryPageBean;
import com.spc.pojo.Address;
import com.spc.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MapService.class)
@Transactional
public class MapServiceImpl implements MapService {
    @Autowired
    private MapDao mapDao;

    @Override
    public List<Address> findAll() {
        List<Address> list= mapDao.findAll();
        return list;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //取出分页条件
        Integer currentPage = queryPageBean.getCurrentPage();//当前页码
        Integer pageSize = queryPageBean.getPageSize();//每页记录数
        String queryString = queryPageBean.getQueryString();//查询条件
        //调用dao完成分页查询，使用mybatis分页助手插件
        PageHelper.startPage(currentPage,pageSize);
        Page<Address> page = mapDao.findByCondition(queryString);
        //使用分页助手中的方法获取总记录数和分页数据
        long total = page.getTotal();
        List<Address> rows = page.getResult();
        PageResult pageResult = new PageResult(total, rows);

        return pageResult;
    }

    @Override
    public List<String> findAddress() {
        List<Address> list= mapDao.findAll();
        List<String> addresses = new ArrayList<String>();
        for (Address address : list) {
            addresses.add(address.getAddress());
        }
        return addresses;
    }

    @Override
    public void deleteById(Integer id) {
        mapDao.deleteById(id);
    }

    @Override
    public void add(Address address) {
        mapDao.add(address);
    }
}
